package org.nonsense;
import java.io.ByteArrayOutputStream;
import java.io.Serializable;
import java.util.Calendar;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;


public class RecorderModel {
	
	public static final int TODAY_ONLY = 0;
	public static final int EVERY_DAY = 1;
	
	public static String[] recordingTypes = { "flac", "wav", "au", "aiff", "aifc", "snd" };
	public static String[] recordingDays = {"Today Only", "Every day", "Mon - Fri", "Sat & Sun", "Sun - Thu", 
		   									"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
	public Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
	private int mixer = 0;
	private TargetDataLine targetDataLine;
	private float sampleRate = 44100.0f;
	private int sampleSizeInBits = 16;
	private int channels = 2;
	private boolean signed = true;
	private boolean bigEndian = false;
	private boolean captureComplete = false;
	
	private AudioInputStream audioInputStream;
	private SourceDataLine sourceDataLine;
	private int outputFormat = 0;	// default to FLAC
	private String outputFilePrefix = "audioRecording";
	
	private String recordStartTime = "12:01";
	private boolean startTimePM = false;
	private String recordEndTime = "12:05";
	private boolean endTimePM = false;
	private String recordOnDays = recordingDays[0];
	private boolean timedRecordingEnabled = false;
	private boolean timedRecordingInProgress = false;
	private Calendar recordStartTimeCal = null;
	private Calendar recordEndTimeCal;
	private int date, month, year = 0;
	
	private String batchFile = "";
	private boolean batchCommandEnabled = false;
	private String archiveDirectory = "F:\\FLACArchive";

	
	public String getBatchFile() {
		return batchFile;
	}

	public void setBatchFile(String batchFile) {
		this.batchFile = batchFile;
	}

	public RecorderModel()	{

	}

	public AudioFormat getAudioFormat()	{
		return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);
    }
	
	public float getSampleRate() {
		return sampleRate;
	}

	public void setSampleRate(float sampleRate) {
		this.sampleRate = sampleRate;
	}

	public int getSampleSizeInBits() {
		return sampleSizeInBits;
	}

	public void setSampleSizeInBits(int sampleSizeInBits) {
		this.sampleSizeInBits = sampleSizeInBits;
	}

	public int getChannels() {
		return channels;
	}

	public void setChannels(int channels) {
		this.channels = channels;
	}

	public boolean isSigned() {
		return signed;
	}

	public void setSigned(boolean signed) {
		this.signed = signed;
	}

	public boolean isBigEndian() {
		return bigEndian;
	}

	public void setBigEndian(boolean bigEndian) {
		this.bigEndian = bigEndian;
	}

	public int getMixer() {
		return mixer;
	}

	public void setMixer(int index) {
		mixer = index;
	}

	public TargetDataLine getTargetDataLine() {
		return targetDataLine;
	}

	public void setTargetDataLine(TargetDataLine targetDataLine) {
		this.targetDataLine = targetDataLine;
	}
	
	public boolean isCaptureComplete()	{
		return captureComplete;
	}
	
	public void captureComplete()	{
		captureComplete = true;
	}

	public AudioInputStream getAudioInputStream() {
		return audioInputStream;
	}

	public void setAudioInputStream(AudioInputStream audioInputStream) {
		this.audioInputStream = audioInputStream;
	}

	public SourceDataLine getSourceDataLine() {
		return sourceDataLine;
	}

	public void setSourceDataLine(SourceDataLine sourceDataLine) {
		this.sourceDataLine = sourceDataLine;
	}

	public void toggleBigEndian() {
		this.bigEndian = !this.bigEndian;
	}

	public void toggleSigned() {
		this.signed = !this.signed;
	}

	public void setOutputFormat(int outputFormat) {
		this.outputFormat = outputFormat;
	}

	public int getOutputFormat() {
		return outputFormat;
	}

	public void setOutputFilePrefix(String text) {
		outputFilePrefix = text;
	}
	
	public String getOutputFilePrefix()	{
		return outputFilePrefix;
	}
	
	public String getRecordStartTime() {
		return recordStartTime;
	}

	public void setRecordStartTime(String recordStartTime) {
		this.recordStartTime = recordStartTime;
		if( date==0 && month==0 && year==0 )	{
			recordStartTimeCal = Calendar.getInstance();
		}
		
		recordStartTimeCal.set(Calendar.HOUR, Integer.parseInt(recordStartTime.substring(0,2)));
		recordStartTimeCal.set(Calendar.MINUTE, Integer.parseInt(recordStartTime.substring(3,5)));
		recordStartTimeCal.set(Calendar.SECOND, 1);
		
	}

	public String getRecordEndTime() {
		return recordEndTime;
	}

	public void setRecordEndTime(String recordEndTime) {
		this.recordEndTime = recordEndTime;
	}

	public String getRecordOnDays() {
		return recordOnDays;
	}

	public void setRecordOnDays(int recordOnDays) {
		this.recordOnDays = recordingDays[recordOnDays];
	}

	public void toggleStartTimePM() {
		startTimePM = !startTimePM;

		if( isStartTimePM() )	{
			recordStartTimeCal.set(Calendar.AM_PM, Calendar.PM);
		}
		else	{
			recordStartTimeCal.set(Calendar.AM_PM, Calendar.AM);
		}
		
	}

	public void toggleEndTimePM() {
		endTimePM = !endTimePM;

		if( isStartTimePM() )	{
			recordEndTimeCal.set(Calendar.AM_PM, Calendar.PM);
		}
		else	{
			recordEndTimeCal.set(Calendar.AM_PM, Calendar.AM);
		}
		
	}

	public void toggleOnOffTimedRecording() {
		timedRecordingEnabled  = !timedRecordingEnabled;
	}

	public boolean isTimedRecordingEnabled()	{
		return timedRecordingEnabled;
	}
	
	public boolean isTimedRecordingInProgress()	{
		return timedRecordingInProgress;
	}
	
	public void toggleTimedRecordingInProgress()	{
		timedRecordingInProgress = !timedRecordingInProgress;
	}

	public boolean isStartTimePM() {
		return startTimePM;
	}

	public void toggleBatchCommandEnabled() {
		batchCommandEnabled = !batchCommandEnabled;
	}
	
	public boolean isBatchCommandEnabled()	{
		return batchCommandEnabled;
	}

	public void setArchiveDirectory(String string) {
		archiveDirectory = string;
	}
	
	public String getArchiveDirectory()	{
		return archiveDirectory;
	}
	
	public Calendar getRecordStartTimeCal() {
		return recordStartTimeCal;
	}

	public void setRecordStartTimeCal(Calendar recordStartTimeCal) {
		this.recordStartTimeCal = recordStartTimeCal;
	}

	public Calendar getRecordEndTimeCal() {
		return recordEndTimeCal;
	}

	public void setRecordEndTimeCal(Calendar recordEndTimeCal) {
		this.recordEndTimeCal = recordEndTimeCal;
	}

	
}
